

#include "reg51.h"
#include "timer.h"

typedef unsigned char BYTE;
typedef unsigned int WORD;

unsigned char hour=23,min=59,sec=50;
#define FOSC 11059200L

#define T1MS (65536-FOSC/12/1000)   //1ms timer calculation method in 12T mode



/* define variables */
WORD count;                         //1000 times counter

//-----------------------------------------------

/* Timer0 interrupt routine */
void tm0_isr() interrupt 1
{
    TL0 = T1MS;                     //reload timer0 low byte
    TH0 = T1MS >> 8;                //reload timer0 high byte
    count++;
    if(count>300)
    {
        count =0;
        sec ++;
        if(sec>59) 
        {
            sec =0;
            min++;
            if(min>59)
            {
                min =0;
                hour++;
                if(hour>23)
                    hour =0;
            }
        }
    }

}

//-----------------------------------------------

/* main program */
void timer0_init()
{
    TMOD = 0x01;                    //set timer0 as mode1 (16-bit)
    TL0 = T1MS;                     //initial timer0 low byte
    TH0 = T1MS >> 8;                //initial timer0 high byte
    TR0 = 1;                        //timer0 start running
    ET0 = 1;                        //enable timer0 interrupt
    EA = 1;                         //open global interrupt switch

}

